//
//  YZModifyNameVC.m
//  VET
//
//  Created by 刘文靖 on 2018/7/6.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZModifyNameVC.h"
#import "YZUser.h"
#import "YZPersonalVM.h"
@interface YZModifyNameVC ()
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (strong, nonatomic) YZPersonalVM *viewModel;
@end

@implementation YZModifyNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewModel = [[YZPersonalVM alloc] init];
    self.nameTF.text = [YZUser sharedInstance].nickName;
    [self.nameTF becomeFirstResponder];
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 36, 25)];
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    [rightButton setTitleColor:kColorWithHex(0x007AFF) forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    [rightButton addTarget:self action:@selector(updateUserName) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *righBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = righBarButtonItem;
}

- (void)updateUserName{
    if ([self.nameTF.text isEqualToString:[YZUser sharedInstance].nickName]){//没有修改
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        @weakify(self)
        [[self.viewModel updteUserInfoWithName:self.nameTF.text] subscribeNext:^(id  _Nullable x) {
            ;
        } completed:^{
            @strongify(self)
             [self.navigationController popViewControllerAnimated:YES];
        }];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

@end
